package com.inei.shapefile_01;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.nocrala.tools.gis.data.esri.shapefile.ShapeFileReader;
import org.nocrala.tools.gis.data.esri.shapefile.exception.InvalidShapeFileException;
import org.nocrala.tools.gis.data.esri.shapefile.header.ShapeFileHeader;
import org.nocrala.tools.gis.data.esri.shapefile.shape.AbstractShape;
import org.nocrala.tools.gis.data.esri.shapefile.shape.PointData;
import org.nocrala.tools.gis.data.esri.shapefile.shape.shapes.MultiPointZShape;
import org.nocrala.tools.gis.data.esri.shapefile.shape.shapes.PointShape;
import org.nocrala.tools.gis.data.esri.shapefile.shape.shapes.PolygonShape;

import com.google.android.gms.maps.model.LatLng;

import android.util.Log;

public class SimpleExample {

	public List<List<LatLng>> lista(FileInputStream fis) throws IOException, InvalidShapeFileException {
		List<List<LatLng>> lista = null;
		ShapeFileReader r = new ShapeFileReader(fis);

		ShapeFileHeader h = r.getHeader();
		Log.i("BIG_FILE", "The shape type of this files is " + h.getShapeType());

		int total = 0;
		AbstractShape s;

		while ((s = r.next()) != null) {
			if (lista == null) {
				lista = new ArrayList<List<LatLng>>();
			}

			switch (s.getShapeType()) {
			case POINT:
				PointShape aPoint = (PointShape) s;
				Log.i("aPoint",aPoint.getY()+" "+aPoint.getX());
				// Do something with the point shape...
				break;
			case MULTIPOINT_Z:
				MultiPointZShape aMultiPointZ = (MultiPointZShape) s;
				// Do something with the MultiPointZ shape...
				break;
			case POLYGON:
				PolygonShape aPolygon = (PolygonShape) s;
				
				Log.i("BIG_FILE",
						"I read a Polygon with " + aPolygon.getNumberOfParts()
								+ " parts and " + aPolygon.getNumberOfPoints()
								+ " points");
				for (int i = 0; i < aPolygon.getNumberOfParts(); i++) {
					PointData[] points = aPolygon.getPointsOfPart(i);
					Log.i("BIG_FILE", "- part " + i + " has " + points.length
							+ " points.");
					Log.i("BIG_FILE", "- point  " + 0 + " has x = " + points[0].getX() + " and y = "+points[0].getY());
					ArrayList<LatLng> poligono = new ArrayList<LatLng>();
					for(int j =0; j<points.length; j++){
						LatLng punto = new LatLng(points[j].getY(), points[j].getX());
						poligono.add(punto);
					}
					lista.add(poligono);
				}
				break;
			default:
				Log.i("BIG_FILE", "Read other type of shape.");
			}
			total++;
		}

		Log.i("BIG_FILE", "Total shapes read: " + total);

		fis.close();
		return lista;
	}

}
